
//获取应用实例  
var app = getApp();
var that;
var common = require("../../utils/common.js");
Page({
  data: {
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: 0, 
    shuaxin:true,
    sta: ["", "待发货", "已发货", "已收货", "取消"],

    pages: [1, 1, 1, 1, 1],  //请求的页面数
    orders:[],

    orderList1: [],
    orderList2: [],
    orderList3: [],
    orderList4: [],
  
    re_jia:0, 
    // status:用currentTab替代//不同状态的订单status:空-全部，1待发货 2已发货 3已完成 4退款中
  },
  onLoad: function (options) {
    that = this
    if (options.cur) {
       that.setData({ status: parseInt(options.cur), currentTab: parseInt(options.cur) })    }
    that.initSystemInfo();
    // that.get_orders()每次进入都会调用bindChange房法，故不需要在此地调用
  },
  initSystemInfo: function () {
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    }); },
  onShow: function () {
    if(that.data.re_jia==1){
      that.data.pages[that.data.currentTab]=1;
      that.setData({ orders: [], orderList1: [], orderList2: [], orderList3: [], orderList4: [] });
      that.get_orders();
    }
  },
  onHide: function () {
    that.setData({re_jia:1});
  },
  bindChange: function (e) {
    that.setData({ currentTab: e.detail.current });
    that.setData({ re_jia: 0 });
    setTimeout(function () {
      that.get_orders();
    }, 300)
   
  },
  swichNav: function (e) {
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var current = e.target.dataset.current;
      that.setData({ currentTab: parseInt(current),});
    };
  },

  get_orders: function () {
    //status:空-全部，1待发货 2已发货 3已完成 4退款中
    var status = that.data.currentTab
    if (status == 0) { status = "" }
    if (status == 4) { status = "-1"}
    app.req(app.local_url + "do=order&p=list",
      { "od": app.data.openid, "status":status, page: that.data.pages[that.data.currentTab] }, function (res) {
        // console.log(res)
      if (res.status == 1) {
        for (var i = 0; i < res.result.list.length;i++){
          if (that.data.currentTab == 0 && res.result.list[i].status!=0) {that.data.orders.push(res.result.list[i])}
          if (that.data.currentTab == 1&&res.result.list[i].status == 1) { that.data.orderList1.push(res.result.list[i]) }
          if (that.data.currentTab == 2&&res.result.list[i].status == 2) { that.data.orderList2.push(res.result.list[i]) }
          if (that.data.currentTab == 3&&res.result.list[i].status == 3) { that.data.orderList3.push(res.result.list[i]) }
          if (that.data.currentTab == 4&&res.result.list[i].status == -1) { that.data.orderList4.push(res.result.list[i]) }
        }
        that.setData({ orders: that.data.orders })
        that.setData({ orderList1: that.data.orderList1 })
        that.setData({ orderList2: that.data.orderList2 })
        that.setData({ orderList3: that.data.orderList3 })
        that.setData({ orderList4: that.data.orderList4 })

        // console.log(that.data.orderList1)
        // console.log(that.data.orderList2)
        // console.log(that.data.orderList3)
        // console.log(that.data.orderList4)
        that.data.pages[that.data.currentTab] = that.data.pages[that.data.currentTab]+1;
        that.setData({pages: that.data.pages })  //更新界面
        setTimeout(function () { that.setData({ shuaxin: true }) }, 1000)
      } else if (res.status == 0) {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_orders()
          }
        })
      }
    })
  },
  tobottom: function () {
    if (that.data.shuaxin) {
      that.setData({ shuaxin: false })
      that.get_orders();
    }
  },


})